#include "parallelogram_button.h"
#include <QPainter>
#include <QPainterPath>

ParallelogramButton::ParallelogramButton(QWidget *parent)
    : QPushButton(parent)
{
}

ParallelogramButton::ParallelogramButton(const QString &text, QWidget *parent)
    : QPushButton(text, parent)
{
}

void ParallelogramButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    
    // 创建平行四边形路径
    QPainterPath path;
    int skew = 12; // 倾斜角度
    int width = this->width();
    int height = this->height();
    
    path.moveTo(skew, 0);
    path.lineTo(width, 0);
    path.lineTo(width - skew, height);
    path.lineTo(0, height);
    path.closeSubpath();
    
    // 设置画笔和画刷
    QColor backgroundColor = isChecked() ? QColor("#2C4675") : 
                            isDown() ? QColor("#3a8ee6") : 
                            isEnabled() ? QColor("#409EFF") : 
                            QColor("#1B2B4D");
    
    painter.setPen(Qt::NoPen);
    painter.setBrush(backgroundColor);
    
    // 绘制平行四边形
    painter.drawPath(path);
    
    // 绘制文字
    painter.setPen(Qt::white);
    QFontMetrics fm(font());
    int textWidth = fm.horizontalAdvance(text());
    int textHeight = fm.height();
    
    // 计算文字位置，使其在平行四边形中居中
    int x = (width - textWidth) / 2;
    int y = (height - textHeight) / 2 + fm.ascent();
    
    painter.drawText(x, y, text());
} 